package cn.jn.lhm.part01;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author lhm 2753914576@qq.com
 * @time 2022/7/3 - 15:50
 **/
public class GatheringWrite {
    public static void main(String[] args) {
        // System.out.println(GatheringWrite.class.getResource("/").getFile());
        // System.out.println(GatheringWrite.class.getResource("/").getPath());
        /**
         * /D:/code/netty/target/classes/
         * /D:/code/netty/target/classes/
         */

        // 集中写
        ByteBuffer buffer1 = StandardCharsets.UTF_8.encode("hello");
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("word");
        ByteBuffer buffer3 = StandardCharsets.UTF_8.encode("你好");

        try (FileChannel channel = new RandomAccessFile(GatheringWrite.class.getResource("/").getPath() + "write.txt", "rw").getChannel()) {
            long size = channel.write(new ByteBuffer[]{buffer1, buffer2, buffer3});
            // 返回写入的字节数
            System.out.println(size);
        } catch (Exception e) {
            // mute
        }
    }
}
